package Concurrency;

import org.apache.commons.lang.ArrayUtils;

import java.util.Map;
import java.util.concurrent.*;

public class BankWaterService implements Runnable {

    private CyclicBarrier cyclicBarrier = new CyclicBarrier(4 , this);

    private Executor executor = Executors.newFixedThreadPool(4);

    private ConcurrentHashMap<String , Integer> sheetBankWaterCount = new ConcurrentHashMap<>();

    private void count() {
        for (int i = 0 ; i < 4 ; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    //执行任务
                    sheetBankWaterCount.put(Thread.currentThread().getName() , 1);
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    @Override
    public void run() {
        int result = 0;
// 汇总每个sheet计算出的结果
        for (Map.Entry<String, Integer> sheet : sheetBankWaterCount.entrySet()) {
            result += sheet.getValue();
        }
// 将结果输出
        sheetBankWaterCount.put("result", result);
        System.out.println(result);

    }

    public static void main(String[] args) {
        BankWaterService bankWaterCount = new BankWaterService();
        bankWaterCount.count();
    }
}
